#!/bin/bash
set -e

TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage1}
SRC=${SRC:-$LFS/sources}

name=gcc-pass1
version=10.2.0
mpfr_ver=4.1.0
gmp_ver=6.2.1
mpc_ver=1.2.1

rm -fr $TMP
mkdir -pv $TMP $LOG

tar xf $SRC/gcc-$version.tar.xz -C $TMP 

{ time \
   {
    
    cd $TMP/gcc-$version
    
    tar -xf $SRC/mpfr-$mpfr_ver.tar.xz
    tar -xf $SRC/gmp-$gmp_ver.tar.xz
    tar -xf $SRC/mpc-$mpc_ver.tar.gz
    
    mv -v mpfr-$mpfr_ver mpfr
    mv -v gmp-$gmp_ver gmp
    mv -v mpc-$mpc_ver mpc

    case $(uname -m) in
    x86_64)
        sed -e '/m64=/s/lib64/lib/' \
            -i.orig gcc/config/i386/t-linux64
    ;;
    esac

    mkdir -v build
    cd       build

    ../configure                                       \
        --target=$LFS_TGT                              \
        --prefix=$LFS/tools                            \
        --with-glibc-version=2.11                      \
        --with-sysroot=$LFS                            \
        --with-newlib                                  \
        --without-headers                              \
        --enable-initfini-array                        \
        --disable-nls                                  \
        --disable-shared                               \
        --disable-multilib                             \
        --disable-decimal-float                        \
        --disable-threads                              \
        --disable-libatomic                            \
        --disable-libgomp                              \
        --disable-libquadmath                          \
        --disable-libssp                               \
        --disable-libvtv                               \
        --disable-libstdcxx                            \
        --enable-languages=c,c++
    
    make
    make install

    cd ..
    cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
        `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/install-tools/include/limits.h

    }
} 2>&1 | tee $name.log

[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
